<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>
      EZ-Offer - Install
    </title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="js/utils.js"></script>
    <style type="text/css">
        form p{
            width: 300px;
        }
        form p input{
            width: 100px;
            float: right;
        }
    </style>
  </head>
  <body>
<?php
require_once('db_structure.php');

if ($_POST['dbsrv']==''){
    echo '<h1>Installation page</h1>
        <form action="'.$_SERVER['PHP_SELF'].'" method="post">
        <p>Database server: <input name="dbsrv" value=""/></p>
        <p>Database login: <input name="dblogin" value=""/></p>
        <p>Database password: <input type="password" name="dbpass" value=""/></p>
        <p>Database name: <input name="dbname" value=""/></p>
        <p><button type="submit">Install</button></p>
        </form>';
} else {
    if ($_POST['dbsrv']!='' && $_POST['dblogin']!='' && $_POST['dbpass']!='' && $_POST['dbname']!='') {
        $link=mysql_connect($_POST['dbsrv'],$_POST['dblogin'],$_POST['dbpass']);
        mysql_select_db($_POST['dbname'],$link) or die('Failed to connect to database.');
        db_struct_create($link);
        mysql_close($link);
        $fp = fopen(dirname(__FILE__).'/../config.php', 'w') or die("\n\n<br>Failed to create config file. Check directory permissions.\n");
        fwrite($fp, "<?php\n");
        fwrite($fp, '$g_sMySqlServer   = \''.$_POST['dbsrv']."';\n");
        fwrite($fp, '$g_sMySqlUsername = \''.$_POST['dblogin']."';\n");
        fwrite($fp, '$g_sMySqlPassword = \''.$_POST['dbpass']."';\n");
        fwrite($fp, '$g_sMySqlDatabase = \''.$_POST['dbname']."';\n");
        fwrite($fp, '?>');
        fclose($fp);
        header("Location: ../index.php");
    } else {
        die('Missing data');
    }
?>
  </body>
</html>